package com.example.teachingSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingSystem.pojo.entity.Student;
import com.example.teachingSystem.pojo.entity.Teacher;
import com.example.teachingSystem.pojo.form.BasePageQuery;
import com.example.teachingSystem.pojo.form.TS.AddStuForm;
import com.example.teachingSystem.pojo.form.TS.UpdateStuForm;
import com.example.teachingSystem.pojo.vo.StudentInfoVo;
import com.example.teachingSystem.service.StudentService;
import com.example.teachingSystem.mapper.StudentMapper;
import com.example.teachingSystem.utils.result.PageResult;
import com.example.teachingSystem.utils.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 86147
* @description 针对表【student(学生表)】的数据库操作Service实现
* @createDate 2023-11-27 21:43:54
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{
    @Autowired
    StudentMapper studentMapper;

    @Override
    public Result<String> adminAddStudent(AddStuForm addStuForm) {
        Student student = new Student();
        System.out.println(addStuForm.getSClass());
        BeanUtils.copyProperties(addStuForm,student);
        Student student1 = studentMapper.selectById(student);
        if(student1!=null){
            return Result.build(null,"添加失败，学号重复");
        }
        if(studentMapper.insert(student)>0){
            return Result.ok("学生添加成功");
        }
        return Result.build(null,"学生添加失败");
    }

    @Override
    public Result<String> adminDeleteStudent(Long stuId) {
        Student student = studentMapper.selectById(stuId);
        if(student==null){
          return  Result.build(null,"查无此人");
        }
        if(studentMapper.deleteById(student)>0){
            return Result.ok("学生删除成功");
        }
        return Result.build(null,"学生删除失败");
    }

    @Override
    public Result<StudentInfoVo> getStudentById(Long stuId) {
        Student student = studentMapper.selectById(stuId);
        if(student==null){
            return Result.build(null,"查无此人");
        }
        StudentInfoVo studentInfoVo = new StudentInfoVo();
        BeanUtils.copyProperties(student,studentInfoVo);
        return Result.ok(studentInfoVo);
    }

    @Override
    public PageResult<StudentInfoVo> adminGetStudent(BasePageQuery basePageQuery) {
        Page<StudentInfoVo> page = new Page<>(basePageQuery.getPageNum(),basePageQuery.getPageSize());
        IPage<StudentInfoVo>iPage = studentMapper.adminGetStudent(page);
        iPage.setTotal(iPage.getRecords().size());
        return PageResult.success(iPage);
    }

    @Override
    public Result<String> adminUpadeStudent(UpdateStuForm updateStuForm) {
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().
                eq(Student::getStudentId,updateStuForm.getStudentId()));
        if(student==null){
            return Result.build(null,500,"学生");
        }
        BeanUtils.copyProperties(updateStuForm,student);
        this.updateById(student);
        return Result.ok(null,"修改成功");
    }
}




